package com.jml.zk原生APi.创建删除修改节点;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

//创建节点1
public class ZkCreateNode implements Watcher{
  public String zkServerPath;
  public Integer timeout;

  @Before
  public void fun(){
     zkServerPath = "localhost:2181";
     timeout = 5000;
  }
  /**
   * 同步或者异步创建节点，都不支持子节点的递归创建，异步有一个callback函数
   * 参数：
   * path：创建的路径
   * data：存储的数据的byte[]
   * acl：控制权限策略
   * 			Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
   * 			CREATOR_ALL_ACL --> auth:user:password:cdrwa
   * createMode：节点类型, 是一个枚举
   * 			PERSISTENT：持久节点
   * 			PERSISTENT_SEQUENTIAL：持久顺序节点
   * 			EPHEMERAL：临时节点
   * 			EPHEMERAL_SEQUENTIAL：临时顺序节点
   */
/*
* 同步创建节点
* */
  @Test
  public void fun1() throws  Exception {
    ZooKeeper zookeeper = new ZooKeeper(zkServerPath, timeout, new ZkCreateNode());
    Thread.sleep(15000);
    String result = zookeeper.create("/testnode1", "testnode".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println("创建节点：\t" + result + "\t成功...");

  }

  /*
  * 异步创建节点
  * */
  @Test
  public void fun2() throws  Exception {
    ZooKeeper zookeeper = new ZooKeeper(zkServerPath, timeout, new ZkCreateNode());
    //这个暂停是留时间连接服务器，连接后才可创建节点
    Thread.sleep(15000);
    String ctx = "{'create':'success'}";
    //异步创建节点
    zookeeper.create("/testnode21", "testnode11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new CreateCallBack(), ctx);
    //这个暂停是留时间创建节点，创建节点后才会调用回调函数
    new Thread().sleep(2000);
  }

  /*
  * 修改zookeeper节点的数据
  * * 参数：
   * path：节点路径
   * data：数据
   * version：数据状态
  * */
  @Test
  public void  fun3() throws  Exception{
    ZooKeeper zookeeper = new ZooKeeper(zkServerPath, timeout, new ZkCreateNode());
    Thread.sleep(15000);
    Stat status  = zookeeper.setData("/testnode", "xyz".getBytes(), 0);
    System.out.println(status.getVersion());
  }

  /*
  * 同步删除节点
  *
  * */
  @Test
  public void fun4() throws  Exception{
    ZooKeeper zookeeper = new ZooKeeper(zkServerPath, timeout, new ZkCreateNode());
    Thread.sleep(15000);
    String result = zookeeper.create("/test-delete-node", "12testnode".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println("创建节点：\t" + result + "\t成功...");
    //节点路径名字，版本号
    zookeeper.delete("/test-delete-node",0);
  }



  /*
   * 异步删除节点
   *
   * */
  @Test
  public void fun5() throws  Exception{
    ZooKeeper zookeeper = new ZooKeeper(zkServerPath, timeout, new ZkCreateNode());
    Thread.sleep(7000);
    String result = zookeeper.create("/test-delete-node1", "12testnode".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println("创建节点：\t" + result + "\t成功...");
    //节点路径名字，版本号
    String ctx = "{'delete':'success'}";
    zookeeper.delete("/test-delete-node1",0,new DeleteCallBack(),"");
    Thread.sleep(2000);
  }





  @Override
  public void process(WatchedEvent watchedEvent) {
    if (watchedEvent.getState() == KeeperState.SyncConnected) {
      System.out.println("zk连接成功...");
    }
  }
}
